<?php

class IndexController extends Zend_Controller_Action
{
    protected $_log;
    
    public function init()
    {
        $this->_log = Zend_Registry::get('log');
    }
/*    
 * @todo Implement proxy settings
 */
    public function indexAction()
    {
        $this->view->headTitle($this->view->translate->_('Login'), 'PREPEND');
     #   $alp = array();
     #   $alp = Zend_Registry::get('afterLoginParams');
     #   var_dump($alp);
     #   die;
        if(Zend_Auth::getInstance()->hasIdentity()) {
            $this->_redirect("index/dashboard");
        }        
        
	$config = new Zend_Config_Ini("/home/marek/BIGProjects/JHUB1ZFTEST/application/forms/loginForm.ini", 'development');
	$form =	new Jhub1_Form($config->user->login);

        if ($this->_request->isPost()) {
            $formData = $this->_request->getPost();
            if ($form->isValid($formData)) {
                $authAdapter = $this->getAuthAdapter();
                $username = $form->getValue('username');
                $password = $form->getValue('password');
                
                $authAdapter->setIdentity($username)->setCredential($password);
                $auth = Zend_Auth::getInstance();
                $result = $auth->authenticate($authAdapter);

                if($result->isValid()) {
                    $identity = $authAdapter->getResultRowObject();
                    $authStorage = $auth->getStorage();
                    $authStorage->write($identity);
                #    die;
                    $this->setRedirection(Zend_Registry::get('afterLoginParams'));
                } else {
                    $form->populate($formData);
                }
            } else {
                $form->populate($formData);
            }
        }
	$this->view->loginForm = $form;
    }

    public function dashboardAction()
    {
	$this->view->headTitle($this->view->translate->_('Dashboard'), 'PREPEND');
    }
    
    public function loginAction()
    {

    }

    public function logoutAction()
    {
        $this->_helper->layout()->disableLayout();
        $this->getHelper('viewRenderer')->setNoRender();
        Zend_Auth::getInstance()->clearIdentity();
        $this->_redirect("index/index");
    }
    private function getAuthAdapter()
    {
        $config = new Zend_Config_Ini("/home/marek/BIGProjects/JHUB1ZFTEST/application/configs/application.ini", 'production');
	$authAdapter = new Zend_Auth_Adapter_DbTable(Zend_Db_Table::getDefaultAdapter());
	$authAdapter->setTableName($config->auth->table)
                    ->setIdentityColumn($config->auth->identityColumn)
                    ->setCredentialColumn($config->auth->credentialColumn);
                    #->setCredentialTreatment($config->auth->credentialTreatment);
        return $authAdapter;
    }
 
    private function setRedirection($alp)
    {
        var_dump($alp);
       # die;
        if (count($alp) > 0) {
            foreach ($alp as $key => $value) {
                if($key != "action" && $key != "controller" && $key != "module") {
                    $options[$key] = $value;   
                }
            }
            $url = $alp['controller'] . "/" . $alp['action'];
            echo $url;
            var_dump($options);
          #  die;
            $this->_redirect($url, $options);
        } else {
            echo "index/dashboard";
         #   die;
            $this->_redirect("index/dashboard");
            
        }
    }
}

